use super::*;

/// 使用欧拉筛加速.
#[allow(unused)]
pub fn min_edge_prime_num(number: u32) -> (usize, usize) {
    const MAX_NUMBER: usize = 80_000_000;
    println!("INFO: MAX_NUMBER: {MAX_NUMBER}");
    let prime_list = euler_sieve(MAX_NUMBER); // TODO 单例化

    let mut count_num = 1;
    let mut count_prime = 0;
    let mut length: usize = 3;
    loop {
        // 统计
        count_num += 4;
        let corner_value = get_corner_value(length as u128);
        for val in corner_value.into_iter() {
            // println!("Check val: {val}");
            if !prime_list[val as usize] {
                // println!("Check val success: {val}");
                count_prime += 1;
            }
        }

        // 检查比例
        let percent = (count_prime * 100 / count_num) as u32;
        // println!("DEBUG: {count_prime} / {count_num} = {percent}%");
        if percent < number {
            return (length, count_prime);
        }

        // 每一层边长加2
        length += 2;
    }
}

/// 使用Euler筛法获取素数信息.
fn euler_sieve(max_number: usize) -> Vec<bool> {
    let mut prime_list = Vec::with_capacity(max_number / 2);
    let mut not_prime: Vec<bool> = vec![false; max_number + 1];

    for i in 2..=max_number {
        if !not_prime[i] {
            prime_list.push(i);
        }
        for p in &prime_list {
            let aim_num = i as u128 * (*p as u128);
            if aim_num > max_number as u128 {
                break;
            }
            not_prime[aim_num as usize] = true;
            if i % p == 0 {
                break;
            }
        }
    }

    not_prime
}
